<?php
/*\
 *  Model - oh.framework
 *  --------------------
 *  name: model
 *  sign: 7
 *	date: 
 *  desc: data abstraction for database data
\*/	oh_module_header();

// Needed library
/*
require_once(dirname(__FILE__)."/util.functions.php");	
require_once(dirname(__FILE__)."/class.functions.php");	
require_once(dirname(__FILE__)."/the_model_args.php");
require_once(dirname(__FILE__)."/the_model_sql.php");
require_once(dirname(__FILE__)."/the_model_meta.php");
require_once(dirname(__FILE__)."/the_model_class.php");
require_once(dirname(__FILE__)."/the_model_object.php");
require_once(dirname(__FILE__)."/the_model.php");
*/
require_once(dirname(__FILE__)."/dmodel.functions.php");	
require_once(dirname(__FILE__)."/dmodel.class.php");

// Interface function   
function oh_module_model($tokens) {
    oh_track_function();
    return oh_this();        
} 

// Main class
class oh_class_model extends the_model { 
	//public $selector;
	//public $model_object_fields;

	public $is_ready;
	public $datasource;

	public $excluded_fields_from_insert_class;

	function oh_class_model() {
		$this->selector = "";
		$this->datasource = oh_datasource_find(array(
			"lara",
			"ezsql",
			"mysql",
			"gladius"				
		));
		oh("config")->set("model.default-prefix","");
		if (!$this->is_installed()) {
			$this->install();
		} 			
		$this->excluded_fields_from_insert_class = array("get");
		
		//$this->model_object_fields = array();
		//$this->model_object_fields["real"] = $this->get_object_fields();
		//$this->model_object_fields["meta"] = array("meta");
		//$this->model_object_fields["chil"] = array();
		//$this->model_object_fields["rsvd"] = array("get");
	}		
	function get_object_fields() {
		return array();
		$col = array();
		$tmp = oh($this->datasource)->get_results("SHOW COLUMNS FROM oh_object");
		foreach($tmp as $c) {
			$col[] = $c->Field;
		}
		return $col;
	}
	function find_datasource() {
		$modules = array(
		);
		foreach($modules as $module) {
			if (oh("debug.modules.model"))
				oh_message("Test module '{$module}' to find 'model datasource'");
			
			if (oh_module_exists($module)) {
				if (oh($module)->is_ready()) {
					return $module;
				}
			}
		}
		return "null";
	}
	function test_installation($prefix="") {
		if ($this->is_installed($prefix)) {
			return true;
		} else {
			$this->install($prefix);
		}
	}
	function get_args_info($args,$model) {
		$info = array();
		if ($model == "object") {
			if (isset($args->form)) {
				$c = $this->get_class($args->form);							
				foreach($args as $key=>$value) {
					$info[$key] = array();	
					if (!in_array($key,$this->model_object_fields["rsvd"])) {
						$info[$key]['is_vald'] = true;
						if (!in_array($key,$this->model_object_fields["real"])) {						
							if (in_array($key,$c->fields["chil"])) {									
								$info[$key]['is_chil'] = true;
								$info[$key]['is_real'] = false;
								$info[$key]['is_meta'] = false;
								$info[$key]['type'] = $key;							
								$info[$key]['name'] = $value;							
							} else {
								$info[$key]['is_chil'] = false;
								$info[$key]['is_real'] = false;
								$info[$key]['is_meta'] = true;
								$info[$key]['meta'] = $key;							
							}							
						} else {
							$info[$key]['is_real'] = true;
							$info[$key]['is_meta'] = false;
							$info[$key]['is_chil'] = false;
						}						
					} else {
						$info[$key]['is_vald'] = false;						
					}
				}
			}
		}
		return $info;
	}
	
	function reinstall() {
		$this->drop("yes");
		$this->install();
	}
	function is_ready() {
		return $this->is_ready;
	}
	function drop($q="datasource") {
		if ($q=="yes") {
			$prefix = $this->get_prefix($prefix);
			if ($this->datasource == "gladius") {
				//$q = "DROP DATABASE ".oh("config.db.name");
				//oh_dump($q);
				//oh($this->datasource)->query($q);
			} else {
				oh($this->datasource)->query("SET foreign_key_checks = 0");
				oh($this->datasource)->query("DROP TABLE {$prefix}class, {$prefix}meta, {$prefix}object");
			}
			//oh_message("Dropped");
		}
		if ($q=="datasource") {
			oh($this->datasource)->drop();
		}			
	}
	function install($prefix = "") {			
		oh_message("Model intalling...");
		//$prefix = $this->get_prefix($prefix);
		$p = oh_model_get_prefix($prefix);
		
		$s = file_get_contents(dirname(__FILE__)."/model.sql");
		$s = explode("--[QUERY]--",$s);
		
		foreach($s as $q) {
			$q = str_replace("__PREFIX__",$p,$q);
			$this->query($q);
		}
		$this->query("INSERT INTO {$p}meta(meta_key,meta_value) VALUES ('oh.module.model.installed','true')");
		/*
		$check = oh($this->datasource)->get_results("SELECT * FROM {$prefix}meta WHERE meta_key='option.db.installed'");			
		if (count($check) == 0) {
			oh($this->datasource)->query("INSERT INTO {$prefix}class (id,name) VALUES (999001,'user')");					

			// Set db installlation complete
			oh($this->datasource)->query("INSERT INTO {$prefix}meta (meta_key,meta_value) VALUES ('option.db.installed','true')");	
		}
		*/			
	}
	function is_installed($prefix="") {			
		$p = oh_model_get_prefix($prefix);		
		$t = $this->query("SELECT meta_value FROM {$p}meta WHERE meta_key='oh.module.model.installed'",true);
		if ($t) {
			$this->is_ready = true;
			return true;
		} else {
			$this->is_ready = false;
			return false;			
		}
	}
	function query($query,$have_results=false) {
		if ($have_results) {
			$data = oh($this->datasource)->get_results($query);						
			return $data;
		} else {
			oh($this->datasource)->query($query);
		}
	}
	function info() {
		?>
		<style>
			.oh-model-info {
				border:1px solid #222;
				padding:3px;
				background:#ccc;
				margin-bottom:1px;
			}			
			.oh-model-default {
				border:1px solid #222;
				padding:3px;
				background:#ddd;
				margin-bottom:1px;
			}			
		</style>
		<div class="oh-model-info">
			Source: <strong><?php echo $this->datasource; ?></strong><br/>
			Database: <strong><?php echo oh("config.db.name"); ?></strong><br/>
			Db username: <strong><?php echo oh("config.db.username"); ?></strong><br/>
			Db password: <strong><?php echo oh("config.db.password"); ?></strong><br/>
			Db host: <strong><?php echo oh("config.db.host"); ?></strong><br/>
			Db path: <strong><?php echo oh("config.db.path"); ?></strong><br/>
		</div>
		<div class="oh-model-default">		
			<?php if ($this->is_installed()) { ?>		
				Default prefix: <strong>Installed</strong><br/>
				Default prefix key: <strong><?php echo $this->get_prefix($prefix); ?></strong><br/>
				Default prefix class:
				<div class="oh-model-class">						
					<?php
						//$c = $this->get_class();
						foreach($c as $k=>$i) {
							echo "<strong>".$i['name']."</strong><br>";
						}
					?>
				</div>
			<?php } else { ?>
				Default prefix: <strong>Not installed</strong><br/>
				Default prefix key: <strong><?php echo $this->get_prefix($prefix); ?></strong><br/>
			<?php } ?>
		</div>
	   	<?php
	}
}
global $oh_object_model;
$oh_object_model = new oh_class_model();
